package com.anjufang.service.system.impl;

import com.anjufang.entity.GroupDetail;
import com.anjufang.entity.GroupInfo;
import com.anjufang.entity.SourceUser;
import com.anjufang.mapper.SourceUserMapper;
import com.anjufang.service.system.SourceDataUserService;
import com.anjufang.service.system.WebApiService;
import com.anjufang.service.user.UserCacheService;
import com.anjufang.utils.RSAEncryptDecrypt;
import com.anjufang.utils.Result;
import com.anjufang.utils.page.Query;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class WebApiServiceImpl implements WebApiService {
	
	@Autowired
	private SourceUserMapper sourceUserMapper;

	@Autowired
	private UserCacheService userCacheService;


	@Override
	public Result draw(String idNumber) {
		try {
			System.out.println("加密后身份证："+idNumber);
			idNumber = new String(RSAEncryptDecrypt.decrypt2(Base64.decodeBase64(idNumber.getBytes("UTF-8"))));
			System.out.println("解密后身份证："+idNumber);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("参数解码错误");
		}
		//拿到用户信息
		SourceUser sourceUser = new SourceUser().selectOne(" id_number = {0} and is_deleted = {1}",idNumber, 0);
		if (null == sourceUser) {
			return Result.error("抱歉，您没有参与资格");
		}
		//判断是第几组
		List<GroupInfo> groupInfos = new GroupInfo().selectList(" is_deleted = {0}",0);
		if(CollectionUtils.isEmpty(groupInfos)){
			return Result.error("活动还没开始，请稍后再来");
		}
		//排序
		groupInfos = groupInfos.stream().sorted(Comparator.comparing(GroupInfo::getId)).collect(Collectors.toList());
		Integer groupSize = groupInfos.get(0).getGroupSize();
		//拿到组信息
		int drawNum = userCacheService.getUserNo(sourceUser.getType());
		/*if(drawNum>=841){
			drawNum = userCacheService.getUserNo(sourceUser.getType());
		}*/
		if (drawNum == 0) {
			return Result.error("编号获取失败，请稍后再试");
		}
		int groupNum = (int)Math.ceil((double)drawNum/groupSize);
		GroupInfo groupInfo = groupInfos.get(groupNum-1);
		if (null == groupInfo) {
			return Result.error("分组失败，请稍后再试");
		}
		//保存到detail
		GroupDetail groupDetail = new GroupDetail();
		groupDetail.setIdNumber(sourceUser.getIdNumber());
		groupDetail.setGroupId(groupInfo.getId());
		groupDetail.setUserId(sourceUser.getId());
		groupDetail.setUserNo(drawNum);
		groupDetail.setUserName(sourceUser.getUserName());
		groupDetail.setType(sourceUser.getType());
		groupDetail.setStatus(0);
		groupDetail.setSortNum(groupDetail.getUserNo()); // 排序同抽签编号
		groupDetail.setCreateTime(new Date());
		groupDetail.setUpdateTime(new Date());
		Boolean draw = groupDetail.insert();
		if(!draw){
			return Result.error("分组失败！请稍后再试");
		}
		return Result.ok().put("groupDetail",groupDetail).put("groupInfo",groupInfo);
	}
}

